 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc_old.EclipseOperationServer;

import java.io.*;
import java.net.*;

public class TEclipseOperationServerCaller implements IEclipseOperationServer { 
	Socket clientSocket; 
	ObjectOutputStream outObjStream;
	ObjectInputStream inObjStream;
	static TEclipseOperationServerCaller singletone = null;

	private TEclipseOperationServerCaller() { 		

	}

	public static TEclipseOperationServerCaller instance() {
		if (singletone == null)
			singletone = new TEclipseOperationServerCaller();
		
		return singletone;
	}

	public VPSDEClassDescription UpdateClassData(VPSDEClassDescription ClassName) {

        try {
            clientSocket = new Socket("localhost", 3128);

            outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
            inObjStream = new ObjectInputStream(clientSocket.getInputStream());
        }
        catch(Exception e) {
        	return null;
        } 

        String data;  
        try
        {
            outObjStream.writeObject(new String("UpdateClassData"));

            outObjStream.writeObject(new String("VPSDEClassDescription"));
            outObjStream.writeObject(ClassName);

            data = (String) inObjStream.readObject();
            if (data.equals("VPSDEClassDescription")) {
                    VPSDEClassDescription UpClass = (VPSDEClassDescription)inObjStream.readObject();
                    return UpClass;
            }

            clientSocket.close();
        } catch (IOException ex ){
        }
        catch (ClassNotFoundException e) {
        }
        return null;

    } 

    public void OpenClassWithEditor(VPSDEClassDescription MainClass) {
        try {
            clientSocket = new Socket("localhost", 3128);

            outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
            inObjStream = new ObjectInputStream(clientSocket.getInputStream());
        }
        catch(Exception e) {}
          
        try {
            outObjStream.writeObject(new String("OpenClassWithEditor"));

            outObjStream.writeObject(new String("VPSDEClassDescription"));
            outObjStream.writeObject(MainClass);

            clientSocket.close();
        } catch (IOException ex ){
        }
    } 

    @Override
    public void SearchTextPattern(VPSDESearchQueryDescription desc) {
    	try {
    		clientSocket = new Socket("localhost", 3128);

    		outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
    		inObjStream = new ObjectInputStream(clientSocket.getInputStream());
    	}
    	catch(Exception e) {
    		System.out.println("init error: "+e);
    	} 

    	try {
    		outObjStream.writeObject(new String("SearchTextPattern"));
    		outObjStream.writeObject(new String("VPSDESearchQueryDescription"));
    		outObjStream.writeObject(desc);

    		clientSocket.close();
    	} catch (IOException ex ){
    	}
    }

	@Override
	public VPSDEClassDescription[] FindClassByName(String[] searchPaths,
			String searchNamePattern) {
    	try {
    		clientSocket = new Socket("localhost", 3128);

    		outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
    		inObjStream = new ObjectInputStream(clientSocket.getInputStream());
    	}
    	catch(Exception e) {
    	} 

    	try {
    		outObjStream.writeObject(new String("FindClassByName"));
    		outObjStream.writeObject(new String("String[]"));
    		outObjStream.writeObject(searchPaths);

    		outObjStream.writeObject(new String("String"));
    		outObjStream.writeObject(searchNamePattern);
            
    		// Read the result
    		String data = (String) inObjStream.readObject();
            if (data.equals("VPSDEClassDescription")) {
                    VPSDEClassDescription[] result = (VPSDEClassDescription[])inObjStream.readObject();
                    clientSocket.close();
                    return result;
            }
    		clientSocket.close();
    	} catch (IOException ex ){
    	} catch (ClassNotFoundException e) {
		}

		return null;
	}

	@Override
	public String FindValueByName(String sourceFile,
			String namePattern) {
		try {
			clientSocket = new Socket("localhost", 3128);

			outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
			inObjStream = new ObjectInputStream(clientSocket.getInputStream());
		}
		catch(Exception e) {
		} 

		try {
			outObjStream.writeObject(new String("FindValueByName"));
			outObjStream.writeObject(new String("String"));
			outObjStream.writeObject(sourceFile);

			outObjStream.writeObject(new String("String"));
			outObjStream.writeObject(namePattern);

			// Read the result
			String data = (String) inObjStream.readObject();
			if (data.equals("String")) {
				String result = (String)inObjStream.readObject();
				clientSocket.close();
				return result;
			}
			clientSocket.close();
		} catch (IOException ex ){
		} catch (ClassNotFoundException e) {
		}

		return null;
	}

	@Override
	public String FindValueByType(String sourceFile,
			String namePattern) {
		try {
			clientSocket = new Socket("localhost", 3128);

			outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
			inObjStream = new ObjectInputStream(clientSocket.getInputStream());
		}
		catch(Exception e) {
		} 

		try {
			outObjStream.writeObject(new String("FindValueByType"));

			outObjStream.writeObject(new String("String"));
			outObjStream.writeObject(sourceFile);

			outObjStream.writeObject(new String("String"));
			outObjStream.writeObject(namePattern);

			// Read the result
			String data = (String) inObjStream.readObject();
			if (data.equals("String")) {
				String result = (String)inObjStream.readObject();
				clientSocket.close();
				return result;
			}
			clientSocket.close();
		} catch (IOException ex ){
		} catch (ClassNotFoundException e) {
		}

		return null;
	}

	
	@Override
	public void OpenMethodImplementation(VPSDEClassDescription className,
			String methodName, String Desc) {
    	try {
    		clientSocket = new Socket("localhost", 3128);

    		outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
    		inObjStream = new ObjectInputStream(clientSocket.getInputStream());
    	}
    	catch(Exception e) {
    	} 

    	try {
    		outObjStream.writeObject(new String("OpenMethodImplementation"));

    		outObjStream.writeObject(new String("VPSDEClassDescription"));
    		outObjStream.writeObject(className);
   
    		outObjStream.writeObject(new String("String"));
    		outObjStream.writeObject(methodName);

    		outObjStream.writeObject(new String("String"));
    		outObjStream.writeObject(Desc);
            
    		clientSocket.close();
    	} catch (IOException ex ){
    	}		
	}

	@Override
	public VPSDEClassDescription[] GetClassRelations(
			VPSDEClassDescription desc, String relationType, String[] searchRestrictions) {

		try {            
			clientSocket = new Socket("localhost", 3128);

			outObjStream = new ObjectOutputStream(clientSocket.getOutputStream());
			inObjStream = new ObjectInputStream(clientSocket.getInputStream());
		} catch(Exception e) {
			return null;
		} 

        String data;  
        try {
            outObjStream.writeObject(new String("GetClassRelations"));

            outObjStream.writeObject(new String("VPSDEClassDescription"));
            outObjStream.writeObject(desc);

            outObjStream.writeObject(new String("String"));
            outObjStream.writeObject(relationType);

            if (searchRestrictions == null) {
            	outObjStream.writeObject(new String("String[]Null"));
            } else {
            	outObjStream.writeObject(new String("String[]"));
            	outObjStream.writeObject(searchRestrictions);
            }

            data = (String) inObjStream.readObject();
            if (data.equals("VPSDEClassDescription[]")) {
            		VPSDEClassDescription[] results = (VPSDEClassDescription[])inObjStream.readObject();  
                    return results;
            }

            clientSocket.close();
        } catch (IOException ex ){
        }
        catch (ClassNotFoundException e) {
        }
        return null;
	}
}  

